﻿
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>DataConverge</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript">
      var xmlHttp;
      var url="";
      var delNode;
      function createXMLHttpRequest() {
			if (xmlHttp == null) {
	    		if (window.ActiveXObject) {
	    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    		}
	    		else if (window.XMLHttpRequest) {
	        		xmlHttp = new XMLHttpRequest();                
	    		}
    		}
	  }
			  
      function changeOrg(){
      		var orgId=document.form0.org.value;
            url="subject.htm?action=getDepartments&orgId="+orgId;
     	    createXMLHttpRequest();
			xmlHttp.open("GET", url, true);
    		xmlHttp.onreadystatechange = callback;
    		xmlHttp.send(null);
      }
      
      function callback(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				clearResults();
    				parseMessage();
    			}
    		}
      }
      
      function parseMessage(){
      		var xmlDoc = xmlHttp.responseXML;
			var nodes = xmlDoc.getElementsByTagName('department');
			if(nodes!=null){
				var id="";
				var name="";
				var res=document.getElementById("departments");
				var input="";
				var text="";
				for(var i=0;i<nodes.length;i++){
					id=nodes[i].firstChild.firstChild.nodeValue;
					name=nodes[i].lastChild.firstChild.nodeValue;
					//alert("id is "+id+" name is "+name);
					input=document.createElement("input");
					input.type="checkbox";
					input.value=id;
					input.name="department";
					res.appendChild(input);
					
					text = document.createTextNode(name);   
					res.appendChild(text); 
				}
			}
			
      }
      
      function clearResults(){
         	var res=document.getElementById("departments");
         	while(res.childNodes.length > 0) {
				res.removeChild(res.childNodes[0]);
			}
      }
      function check(){
        var org=document.getElementById("org").value;
        var outOrg=document.getElementById("outOrgName").value;
        //alert(org);
        //alert(outOrg);
        if(org!=''&&outOrg!=''){
           alert("请在“单位”和“外单位”中二选一！");
        }else{
           addDepartments();
           }
           
      }
      function addDepartments(){
        var ids="";
        var nodes=document.getElementById("departments").childNodes;
      	for(i=0;i<nodes.length;i++){
			if(nodes[i].checked==true){
				ids=ids+nodes[i].value+";";
				i++;
			}
		}
		var orgId=document.form0.org.value;
		url="subject.htm?action=addOrgs&departmentIds="+ids+"&orgId="+orgId+"&role="+escape(document.form0.role.value)+"&outOrgName="+escape(document.form0.outOrgName.value);;
		createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
    	xmlHttp.onreadystatechange = callbackForAddOrg;
    	xmlHttp.send(null);
      }
      
      function callbackForAddOrg(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				var xmlDoc = xmlHttp.responseXML;
					var nodes = xmlDoc.getElementsByTagName('orgAdded');
					var added=nodes[0].firstChild.nodeValue;					
    				if(added>0){
    					alert("已成功添加"+added+"个单位部门!");
    					var page=document.form0.page.value;
      					var id=document.form0.subjectId.value;
      					document.location.href = "subject.htm?action=gotoAddOrg&subjectId="+id+"&page="+page;
      				}
      				else{
      					var errMsg=xmlDoc.getElementsByTagName('errMsg')[0].firstChild.nodeValue;
      					alert(errMsg);
      				}
    			}
    	}
      }
      
      function delSubOrg(subOrgId,node){
      	if(!window.confirm("确定删除？","确定","取消"))	
        return;
      	url="subject.htm?action=delSubOrg&subOrgId="+subOrgId;
      	delNode=node.parentNode.parentNode;
      	createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
    	xmlHttp.onreadystatechange = callbackForDelSubOrg;
    	xmlHttp.send(null);
      }
      
      function callbackForDelSubOrg(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				var xmlDoc = xmlHttp.responseXML;
					var nodes = xmlDoc.getElementsByTagName('subOrgDeleted');
					var deleted=nodes[0].firstChild.nodeValue;					
    				if(deleted==1){
    					delNode.parentNode.removeChild(delNode);
    				}else{
      					var errMsg=xmlDoc.getElementsByTagName('errMsg')[0].firstChild.nodeValue;
      					alert(errMsg);
      				}
    			}
    	}
      }
          
     function goback(){

      }
    </script>
  </head>
  
  <body>
    <br>
    <DIV align="center"><strong>已加课题单位信息</strong></DIV>
    <TABLE border="1" width="70%" align="center">
   		<TR>
   			<TD>单位名称</TD>
   			<TD>部门名称</TD>
   			<TD>外单位名</TD>
   			<TD>单位角色</TD>
   			<c:if test="${subject.state!='1'}">
   			<TD>删除</TD>
   			</c:if>
   		</TR>
   		<c:forEach items="${subOrgList}" var="subOrg">
   		<TR>
   			<td>
			  <c:forEach items="${orgList}" var="org" varStatus="loopStep">
    				<c:if test="${subOrg.orgId==org.id}"><c:out value="${org.name}"/></c:if>
    		  </c:forEach>&nbsp;
    		</td>
    		<td>
    		 <c:forEach items="${depList}" var="dep" varStatus="loopStep">
    				<c:if test="${subOrg.depId==dep.id}"><c:out value="${dep.name}"/></c:if>
    		  </c:forEach>&nbsp;
    		</td>
    		<td><c:out value="${subOrg.outOrgName}"/>&nbsp;</td>
   			<td><c:out value="${subOrg.orgRole}"/></td>
   			<c:if test="${subject.state!='1'}">
   			<td><a href="#" onclick="delSubOrg(${subOrg.id},this);"><img src="images/delete.gif"/></a></td>
   			</c:if>
   		</TR>
   		</c:forEach>
   		
    </TABLE>
    <br>
    <DIV align="center"><h4>添加单位</h4></DIV>
    <FORM name="form0">
    	<input type="hidden" name="page" value="${page}"/>
    	<input type="hidden" name="subjectId" value="${subjectId}"/>
    	<TABLE border="1" width="70%" align="center">
    		<TR>
    			<TD>单位：</TD>
    			<TD><select name="org" onchange="changeOrg();">
    					<option value=""></option>
    				<c:forEach items="${orgList}" var="org" varStatus="loopStep">
    					<option value="${org.id}" <c:if test="${currentOrgId==org.id}">selected</c:if>
    					><c:out value="${org.name}"/></option>;
    				</c:forEach>
    			    </select>
    			</TD>
    		</TR>
    		<TR>
    			<TD>可选部门：</TD>
    			<TD id="departments">
    			<c:forEach items="${orgDepList}" var="dep">
    			<input type="checkbox" name="department" value="${dep.id}" />${dep.name}
    			</c:forEach>
                </TD>
    		</TR>
    		<TR>
    			<TD>外单位：</TD>
    			<TD><input type="text" name="outOrgName" size="40"></TD>
    		</TR>
    		<TR>
    			<TD>角色：</TD>
    			<TD><select name="role">
    					<option value="未知"></option>
    					<option value="牵头单位">牵头单位</option>
    					<option value="合作单位">合作单位</option>
    				</select>
    			</TD>
    		</TR>
    	</TABLE>
    	<br/>
    	<TABLE align="center">
    		<TR>
    			<TD>
    			<c:if test="${subject.state!='1'}">
    			<input type="button" value="添加" onclick="check();">&nbsp;&nbsp;
    			</c:if>
    				<input type="button" value="返回" onclick="history.back(-1)">
    			</TD>
    		</TR>
    	</TABLE>
    </FORM>
  </body>
</html>
